// ActionScript file
import com.octlan.wbcompare.base.Constants;
import com.octlan.wbcompare.ctrl.MainCtrl;
import com.octlan.wbcompare.util.XMLHelper;
import com.octlan.wbcompare.view.DataWindow;

import flash.events.Event;
import flash.net.URLVariables;

import mx.charts.HitData;
import mx.charts.renderers.CircleItemRenderer;
import mx.charts.series.LineSeries;
import mx.core.ClassFactory;
import mx.graphics.Stroke;
import mx.managers.PopUpManager;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;

private function init():void
{
    loadCountry();
    loadIndicator();
}

private var cs:HTTPService;
private function loadCountry():void
{
    var ctrl:MainCtrl = MainCtrl.getInstance();
    cs = ctrl.createService();
    cs.url = Constants.SERVER_URI;
    cs.addEventListener(ResultEvent.RESULT, onCountryResult);

    var paras:URLVariables = new URLVariables();
    paras.catalog = 'country';
    paras.per_page = 300;
    paras.page = 1;

    cs.send(paras);
}
[Bindable]
private var countrylist:Array = new Array();

private function onCountryResult(event:ResultEvent):void
{
    cs.removeEventListener(ResultEvent.RESULT, onCountryResult);
    cs = null;

    countrylist = new Array();
    var str:String = event.result.toString();
    var info:XML = XMLHelper.removeNamespaceURI(str);
    for each (var item:XML in info.children())
    {
        var obj:Object = new Object();
        obj.value = item.@id;
        obj.name = item.name;
        obj.cname = item.cname + "(" + item.name + ")";
        obj.isSelected = false;
        countrylist.push(obj);
    }
    country.dataProvider = countrylist;
}

private var indService:HTTPService;
private function loadIndicator():void
{
    var ctrl:MainCtrl = MainCtrl.getInstance();
    indService = ctrl.createService();
    indService.url = Constants.SERVER_URI;
    indService.addEventListener(ResultEvent.RESULT, onIndicatorResult);

    var paras:URLVariables = new URLVariables();
    paras.catalog = 'indicators';
    paras.per_page = 300;
    paras.page = 1;

    indService.send(paras);
}

[Bindable]
private var indicatorlist:Array = new Array();
private function onIndicatorResult(event:ResultEvent):void
{
    indService.removeEventListener(ResultEvent.RESULT, onIndicatorResult);
    indService = null;

    indicatorlist = new Array();
    var str:String = event.result.toString();
    var info:XML = XMLHelper.removeNamespaceURI(str);
    for each (var item:XML in info.children())
    {
        var obj:Object = new Object();
        obj.value = item.@id;
        obj.name = item.name;
        obj.cname = item.cname; // +"(" + item.name +")";
        indicatorlist.push(obj);
    }
    indicator.dataProvider = indicatorlist;
    url.text = indicator.selectedItem.name;
}

private var ds:HTTPService;
private function generateUrl():void
{
    lineChart.series = new Array();
    xmlData = "";

    for each (var item:Object in countrylist)
    {
        if (item.isSelected)
        {
            getData(item.value);
        }
    }
}

private function indicatorChange(event:Event):void
{
    url.text = indicator.selectedItem.name;
}

private var xmlData:String;
private function showDataXML():void
{
    var infoWindow:DataWindow = new DataWindow();
    PopUpManager.addPopUp(infoWindow, this, true);
    PopUpManager.centerPopUp(infoWindow);
    infoWindow.setInfoText(xmlData);
}

private function getData(country:String):void
{
    var str:String = "countries";
    str = str + "/" + country;
    str = str + "/" + "indicators";
    str = str + "/" + indicator.selectedItem.value;
    //url.text = str;

    //var dr:String = dfrom.text + ":" + dto.text;

    var ctrl:MainCtrl = MainCtrl.getInstance();
    ds = ctrl.createService();
    ds.url = Constants.SERVER_URI;
    ds.addEventListener(ResultEvent.RESULT, onDataResult);

    var paras:URLVariables = new URLVariables();
    paras.catalog = str;
    paras.per_page = 300;
    paras.page = 1;
    //paras.date = dr;

    ds.send(paras);
}

//static var defaultColors:Array = [0xF03439, 0x484DB1, 0xFFC126, 0x5FB1EF, 0xD85FC6, 0x666666, 0x92D8D1, 0xE48701, 0xA5BC4E, 0xCACA9E, 0xF05E27, 0xE4F9A0, 0xFFD512, 0x75B000, 0x0662B0, 0xEDE8C6, 0xCC3300, 0xD1DFE7, 0x52D4CA, 0xC5E05D, 0xE7C174, 0xFFF797, 0xC5F68F, 0xBDF1E6, 0x9E987D, 0xEB988D, 0x91C9E5, 0x93DC4A, 0xFFB900, 0x9EBBCD, 0x009797, 0x0DB2C2];


private function onDataResult(event:ResultEvent):void
{
    //ds.removeEventListener(ResultEvent.RESULT, onDataResult);
    //ds = null;
    var localSeries:LineSeries = new LineSeries();
    localSeries.interpolateValues = true;

    var str:String = event.result.toString();
    var info:XML = XMLHelper.removeNamespaceURI(str);
    xmlData = xmlData + info.toString();
    var datalist:Array = new Array();
    for each (var item:XML in info.children())
    {
        datalist.unshift(item);
        localSeries.displayName = item.country;
    }

    horizontalAxis.dataProvider = datalist;
    localSeries.dataProvider = datalist;
    localSeries.xField = "date";
    localSeries.yField = "value";
    var stroke:Stroke = new Stroke();
    stroke.color = 0xFFFFFF * Math.random();
    stroke.weight = 1;
    stroke.alpha = 1;
    localSeries.setStyle("lineStroke", stroke);
    localSeries.setStyle("form", "curve");

    //localSeries.setStyle("fill", 0x000000);
    //localSeries.setStyle("radius", 10);
    //localSeries.setStyle("adjustedRadius", 2);
    localSeries.setStyle("itemRenderer", new ClassFactory(CircleItemRenderer));


    // Back up the current series on the chart.
    var currentSeries:Array = lineChart.series;
    // Add the new series to the current Array of series.
    currentSeries.push(localSeries);
    // Add the new Array of series to the chart.
    lineChart.series = currentSeries;
}

private function dtFunc(hd:HitData):String
{
    return hd.item.country + "\n" + hd.item.date + "\n" + hd.item.value;
}

